package cn.jinjimi.app.custom.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventDataField;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.controller.CustomerController;
import cn.jinjimi.app.custom.event.InsuranceAddEvent;
import cn.jinjimi.app.main.dialog.CommAlertDialog;
import cn.jinjimi.app.main.dialog.DatePickerDialog;
import cn.jinjimi.app.main.dialog.LoadingDialog;
import cn.jinjimi.app.main.widget.LunarDatePicker;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Calendar;

import static cn.jinjimi.app.R.id.add_insure_edt_insurance_name;

/**
 * 新增保单提醒
 * <p/>
 * Created by Jinsen on 2015/8/21.
 */
public class AddInsureDialog extends Dialog implements View.OnClickListener {

    private Context mContext = null;

    private EditText mInsureName = null;
    private TextView mInsureDate = null;

    private long mCustomId = 0;

    private Calendar mInsuranceEffectDate = null;

    private DatePickerDialog mDateDialog = null;
    private int mDateType = DatePickerDialog.OnDateSetListener.TYPE_GREGORIAN_YEAR;

    public AddInsureDialog(Context context, long customId) {
        super(context);
        mContext = context;
        this.mCustomId = customId;

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_add_insurance_layout);

        findViewById(R.id.add_insure_close).setOnClickListener(this);
        findViewById(R.id.add_insure_save).setOnClickListener(this);

        mInsureName = (EditText) findViewById(add_insure_edt_insurance_name);
        mInsureDate = (TextView) findViewById(R.id.add_insure_edt_insurance_date);
        mInsureDate.setOnClickListener(this);

        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(-1, -2);
        setCanceledOnTouchOutside(false);
        EventBus.getDefault().register(this);
    }

    private void destory() {
        mDateDialog = null;
        mContext = null;
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        destory();
    }

    @EventTrace(value = EventID.APP_CUSTOM_POLICY_ADDPOLICY, paramsK = {EventDataField.ParamType}, paramsV = {EventDataField.ClickCancel})
    private void cancleAddInsure() {
        dismiss();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_insure_close:
                if (StringUtils.isEmpty(mInsureName.getText().toString().trim()) && mInsuranceEffectDate == null) {
                    cancleAddInsure();
                } else {
                    cancelEditAlert();
                }

                break;
            case R.id.add_insure_save:
                String insurancename = mInsureName.getText().toString().trim();
                if (StringUtils.isEmpty(insurancename)) {
                    ToastUtils.toastMessage(mContext, mContext.getString(R.string.add_insurance_need_input_name));
                    return;
                }

                if (mInsuranceEffectDate != null) {
                    if (mInsuranceEffectDate.before(Calendar.getInstance())) {
                        addInsurance(insurancename);
                    } else {
                        ToastUtils.toastMessage(mContext, mContext.getString(R.string.add_insurance_input_date_invalid));
                    }
                } else {
                    ToastUtils.toastMessage(mContext, mContext.getString(R.string.add_insurance_input_date_hint));
                }
                break;

            case R.id.add_insure_edt_insurance_date:
                showDatePickerDialog();
                break;
        }
    }

    @EventTrace(value = EventID.APP_CUSTOM_POLICY_ADDPOLICY, paramsK = {EventDataField.ParamType}, paramsV = {EventDataField.ClickConfirm})
    private void addInsurance(String insurancename) {
        CustomerController.addInsurance(String.valueOf(mCustomId), insurancename, mInsuranceEffectDate.getTimeInMillis(), 12);
        LoadingDialog.show(mContext, mContext.getString(R.string.add_insurance_ing));
    }

    /**
     * 弹出确定框
     */
    private void cancelEditAlert() {
        final CommAlertDialog mAlertDialog = new CommAlertDialog(mContext, R.style.TransparentDialog);
        mAlertDialog.setTitle(mContext.getString(R.string.giveup_edit))
                .setMessage(mContext.getString(R.string.giveup_edit_msg))
                .setRightBtn(mContext.getString(R.string.ok), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        cancleAddInsure();
                        mAlertDialog.dismissDialog();
                    }
                });
        mAlertDialog.setCanceledOnTouchOutside(false);
        mAlertDialog.showDialog(CommAlertDialog.Style.TWO_BUTTON);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onInsuranceAddEvent(InsuranceAddEvent event) {
        switch (event.state) {
            case InsuranceAddEvent.SUCC:
                dismiss();
                break;
            case InsuranceAddEvent.FAIL:
                break;
        }
    }

    private void showDatePickerDialog() {
        Calendar today = Calendar.getInstance();
        if (mInsuranceEffectDate != null)
            today = mInsuranceEffectDate;

        if (mDateDialog == null) {
            mDateDialog = new DatePickerDialog(mContext,
                    mDatelistener, mDateType,
                    today.get(Calendar.YEAR),
                    today.get(Calendar.MONTH),
                    today.get(Calendar.DAY_OF_MONTH));
            mDateDialog.hideYearSwitchAndTypeSel();
        }

        mDateDialog.updateDate(today.get(Calendar.YEAR),
                today.get(Calendar.MONTH),
                today.get(Calendar.DAY_OF_MONTH),
                mDateType);
        mDateDialog.show();
    }

    private DatePickerDialog.OnDateSetListener mDatelistener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(LunarDatePicker view, Calendar date, String dateStr, int type) {
            mDateType = type;
            mInsuranceEffectDate = date;
            mInsureDate.setText(dateStr);
        }
    };
}
